//
//  TSCVCStory.m
//  Tsc_IOS
//
//  Created by Rio on 15-5-12.
//  Copyright (c) 2015年 idream. All rights reserved.
//

#import "TSCVCStory.h"
#import "TSCSDK.h"
#import "TSCVCStoryPage.h"

@interface TSCVCStory ()

@end

@implementation TSCVCStory {
    // 输入参数
    int _iStoryVersion;
    
    NSInteger _userRole;
    NSUInteger _totalPageNum; // 总页数，在viewDidLoad()中，根据_vcTitleArr自动计算
}

#pragma mark 校验并获取输入
- (void) getVCInput:(NSNotificationCenter *)notification {
    [super getVCInput:notification];
    
    _iStoryVersion = [[_inputDic objectForKey:AEKI_STORY_VERSION] intValue];
    
}

+ (id)vcStoryWithStoryboard {
    UIStoryboard * sb = [UIStoryboard storyboardWithName:UI_STORYBOARD_NAME_MAIN bundle:[NSBundle mainBundle]];
    return [sb instantiateViewControllerWithIdentifier:UI_VC_NAME_STORY];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 获得SP
    _userRole = [[_ud objectForKey:UD_USER_ROLE] integerValue];
    
    // 初始化属性
    _vcArr = [NSMutableArray array];
    if (_iStoryVersion == TSCStoryVersionBrief) {
        if (_userRole == DB_USER_ROLE_CUSTOMER) {
            _vcTitleArr = [NSArray arrayWithObjects:
                           @"我找商品 -> 商品找我",
                           @"商场精品，货真价实！",
                           @"全城比价，好货不用到处跑！",
                           @"跨城购物，从此土豪任性到底！",
                           @"中国好男友，都会用商场快车！",
                           @"周边店铺，下班就去逛！",
                           nil];
            _vcImgNameArr = [NSArray arrayWithObjects:
                             @"story_customer_1_r_480x650.png",
                             @"story_customer_2_r_480x650.png",
                             @"story_customer_4_r_480x650.png",
                             @"story_customer_6_r_480x650.png",
                             @"story_customer_7_r_480x650.png",
                             @"story_customer_8_r_480x650.png",
                             nil];
            _vcBtnTextArr = [NSArray arrayWithObjects:
                             @">>>",
                             @">>>",
                             @">>>",
                             @">>>",
                             @">>>",
                             [NSString stringWithFormat:@">>>进入%@>>>", NSLocalizedString(@"app_name", nil)],
                             nil];
            
        } else if(_userRole == DB_USER_ROLE_GUIDE) {
            _vcTitleArr = [NSArray arrayWithObjects:
                           @"足不出店，照样招揽生意！",
                           @"顾客精准推送，成交近在咫尺",
                           @"利用闲暇时间，成就人生赢家",
                           @"业绩提升，店长夸、奖金大把、心里乐得一朵花！",
                           nil];
            _vcImgNameArr = [NSArray arrayWithObjects:
                             @"story_guide_1_r_480x650.png",
                             @"story_guide_2_r_480x650.png",
                             @"story_guide_3_r_480x650.png",
                             @"story_guide_4_r_480x650.png",
                             nil];
            _vcBtnTextArr = [NSArray arrayWithObjects:
                             @">>>",
                             @">>>",
                             @">>>",
                             [NSString stringWithFormat:@">>>进入%@>>>", NSLocalizedString(@"app_name", nil)],
                             nil];
        } else {
            NSAssert(YES == NO, @"[E]%s: 显示“我们的故事”界面时，发现ud中保存了非法的用户身份：%li！", __FUNCTION__, _userRole);
        }
        
    }
    
    // 初始化总页数
    _totalPageNum = [_vcTitleArr count];
    
    // 创建第一页
    TSCVCStoryPage * vcSP0 = [TSCVCStoryPage vcStoryPageWithStoryboard];
    vcSP0.pvc = _pvc;
    vcSP0.pageIndex = 0;
    vcSP0.pageTitle = _vcTitleArr[0];
    vcSP0.imgName = _vcImgNameArr[0];
    vcSP0.btnText = _vcBtnTextArr[0];
    vcSP0.totalPageNum = _totalPageNum;
    
    // 将第一页添加到_vcArr中
    [_vcArr addObject:vcSP0];
    
    // 初始化pvc
    _pvc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
                                           navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                                         options: nil];
    [_pvc setViewControllers:_vcArr
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:YES
                  completion:nil];
    _pvc.dataSource = self;
    
    // 在页面上，显示UIPageViewController对象的View
    [self addChildViewController:_pvc];
    [self.view addSubview:_pvc.view];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

#pragma mark pageViewController代理

// 获得上一页
- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    // 转换vc类型
    TSCVCStoryPage * vcSP = (TSCVCStoryPage *) viewController;
    
    // 找到当前页、上一页vc的index
    NSInteger index = vcSP.pageIndex;
    NSInteger preIndex = index - 1;
    
    // 参数检查
    NSAssert(_totalPageNum > 0, @"[E]%s: 获得“我们的故事”上一页时，页总数必须大于0！", __FUNCTION__);
    NSAssert(index >= 0 && index <= _totalPageNum - 1, @"[E]%s: 获得“我们的故事”上一页时，当前页下标[%li]不在合理范围[0, %li]内！", __FUNCTION__, index, _totalPageNum - 1);
    
    
    // 上一页根本不存在
    if (preIndex < 0 || preIndex > _totalPageNum - 1) { // TODO 后半个判断应该没必要
        return nil;
    }
    // 上一页存在，再看看是否已缓存
    else {
        // 在缓存中存在
        if (preIndex >= 0 && preIndex <= [_vcArr count] - 1) {
            return _vcArr[preIndex];
        }
        // 在缓存中不存在
        else {
            // 创建上一页
            TSCVCStoryPage * vcSPPre = [TSCVCStoryPage vcStoryPageWithStoryboard];
            vcSPPre.pvc = _pvc;
            vcSPPre.pageIndex = preIndex;
            vcSPPre.pageTitle = _vcTitleArr[preIndex];
            vcSPPre.imgName = _vcImgNameArr[preIndex];
            vcSPPre.btnText = _vcBtnTextArr[preIndex];
            vcSPPre.totalPageNum = _totalPageNum;
            
            // 添加到缓存
            [_vcArr addObject:vcSPPre];
            
            return vcSPPre;
        }
    }
}

// 获得下一页
- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    // 转换vc类型
    TSCVCStoryPage * vcSP = (TSCVCStoryPage *) viewController;
    
    // 找到当前页、下一页vc的index
    NSInteger index = vcSP.pageIndex;
    NSInteger nextIndex = index + 1;
    
    // 参数检查
    NSAssert(_totalPageNum > 0, @"[E]%s: 获得“我们的故事”下一页时，页总数必须大于0！", __FUNCTION__);
    NSAssert(index >= 0 && index <= _totalPageNum - 1, @"[E]%s: 获得“我们的故事”下一页时，当前页下标[%li]不在合理范围[0, %li]内！", __FUNCTION__, index, _totalPageNum - 1);
    
    
    // 下一页根本不存在
    if (nextIndex < 0 || nextIndex > _totalPageNum - 1) { // TODO 前半个判断应该没必要
        return nil;
    }
    // 下一页存在，再看看是否已缓存
    else {
        // 在缓存中存在
        if (nextIndex >= 0 && nextIndex <= [_vcArr count] - 1) {
            return _vcArr[nextIndex];
        }
        // 在缓存中不存在
        else {
            // 创建下一页
            TSCVCStoryPage * vcSPNext = [TSCVCStoryPage vcStoryPageWithStoryboard];
            vcSPNext.pvc = _pvc;
            vcSPNext.pageIndex = nextIndex;
            vcSPNext.pageTitle = _vcTitleArr[nextIndex];
            vcSPNext.imgName = _vcImgNameArr[nextIndex];
            vcSPNext.btnText = _vcBtnTextArr[nextIndex];
            vcSPNext.totalPageNum = _totalPageNum;
            
            // 添加到缓存
            [_vcArr addObject:vcSPNext];
            
            return vcSPNext;
        }
    }
}

@end
